# ------------------------------------------------------------------------------
# Builds longterm (31-365 day) adverse outcome variables
# Updates author: Cassidy Shubatt <cshubatt@gmail.com>
# To run: bash 13_build_longterm_outcomes.sh
# ------------------------------------------------------------------------------

# Libraries --------------------------------------------------------------------
library(here)
library(yaml)
library(tidyverse)
library(glue)
library(reticulate)
library(data.table)

u <- modules::use(here("lib", "util.R"))
source_python(here::here("lib", "python_util.py"))
overnight_lab <- ""

# Load Data --------------------------------------------------------------------
message("Loading data...")
paths <- read_yaml(here("lib", "filepaths.yml"))
daly_cost <- readRDS(paths$analysis$daly_cost) %>% select(ed_enc_id, cost)
life_exp <- readRDS(paths$analysis$life_expectancy)
cohort <- readRDS(glue(paths$analysis$full_cohort)) %>%
  select(ptid, ed_enc_id, start_datetime, end_datetime) %>%
  mutate(
    start_datetime = case_when(
      is.na(start_datetime) ~ end_datetime,
      TRUE ~ start_datetime
    )
  )
troponin <- read_parquet_file(paths$raw_cohort$troponin_labs)

death <- readRDS(paths$analysis$demographics) %>%
  select(
    ed_enc_id, days_to_death, death_030_day, death_060_day, death_365_day
  ) %>%
  mutate(death_090_day = (days_to_death <= 90) %>% replace_na(FALSE)) %>%
  mutate(death_180_day = (days_to_death <= 180) %>% replace_na(FALSE)) %>%
  mutate(death_730_day = (days_to_death <= 730) %>% replace_na(FALSE))

# Xwalk troponin ---------------------------------------------------------------
message("Crosswalking longterm tn to encounters...")
min_date <- min(cohort$start_datetime, na.rm = TRUE)
# min_time <- as.difftime(30, units = "days")
max_time <- as.difftime(365, units = "days")
tn_df <- troponin %>%
  filter(lab_value_mod > 0 & lab_date >= min_date)
tn_xwalk <- cohort %>%
  left_join(tn_df) %>%
  mutate(days_to_tn = lab_date - start_datetime) %>%
  filter(
    days_to_tn <= max_time
  )

tn_xwalk_collapsed <- cohort
for(min_num_days in c( -1, 30, 60, 90)){
  for (tn_cutoff in c(0, 0.05, 0.1, 0.5)) {
    cutoff_name <- case_when(
      tn_cutoff == 0 ~ "pos",
      TRUE ~ as.character(tn_cutoff)
    )

    min_time <- as.difftime(min_num_days, units = "days")
    min_days_num <- min_num_days + 1
    trop_varname <- glue(
      "macetrop_{min_days_num}_to_365_{cutoff_name}"
    )
    if(min_num_days == -1){
      trop_varname <- glue("macetrop_365_{cutoff_name}")
    }

    xwalk <- tn_xwalk %>%
      mutate(
        macetrop_min_to_365 = lab_value_mod > tn_cutoff & days_to_tn > min_time
      ) %>%
      group_by(ed_enc_id) %>%
      summarize(
        macetrop_min_to_365 = any(macetrop_min_to_365)
      ) %>%
      ungroup() %>%
      select(ed_enc_id, macetrop_min_to_365)

    tn_xwalk_collapsed <- tn_xwalk_collapsed %>%
      u$safe_left_join(xwalk) %>%
      mutate(macetrop_min_to_365 = replace_na(macetrop_min_to_365, FALSE)) %>%
      setnames("macetrop_min_to_365", trop_varname)
  }
}

# Create longterm variables ----------------------------------------------------
message("Creating longterm variables...")
longterm <- cohort %>%
  select(ptid, ed_enc_id) %>%
  u$safe_left_join(tn_xwalk_collapsed) %>%
  u$safe_left_join(life_exp) %>%
  u$safe_left_join(death) %>%
  select(
    ed_enc_id, macetrop_31_to_365_0.1, macetrop_31_to_365_0.05,
    macetrop_31_to_365_pos, macetrop_61_to_365_pos,
    macetrop_91_to_365_pos, macetrop_365_pos,
    death_030_day, death_060_day, death_090_day, death_365_day,
    death_180_day, death_730_day, ly_remaining
  ) %>%
  mutate(
    ly_remaining = case_when(
      death_365_day ~ 0,
      macetrop_365_pos ~ 0.875 * ly_remaining,
      TRUE ~ ly_remaining
    )
  ) %>%
  mutate(
    death_31_to_180 = death_180_day & !death_030_day,
    death_31_to_365 = death_365_day & !death_030_day,
    death_31_to_730 = death_730_day & !death_030_day,
    death_61_to_365 = death_365_day & !death_060_day,
    death_91_to_365 = death_365_day & !death_090_day,
    macetrop_or_death_31_to_365 = macetrop_31_to_365_pos | death_31_to_365,
    macetrop_or_death_61_to_365 = macetrop_61_to_365_pos | death_61_to_365,
    macetrop_or_death_91_to_365 = macetrop_91_to_365_pos | death_91_to_365,
    macetrop_or_death_365 = macetrop_365_pos | death_365_day
  )

# Save -------------------------------------------------------------------------
message("Saving...")
write_rds(longterm, paths$analysis$longterm_outcomes)

message("Done.")
